﻿

function getPageList(pageCount, pageCurrent, parameterKey) {
    pageCount = parseInt(pageCount);
    pageCurrent = parseInt(pageCurrent);

    //var pageCurrent = parseInt(getQueryStringParameter("p"));
    // chỉnh trang hiện tại
    if (pageCurrent > pageCount) {
        pageCurrent = pageCount;
    }
    if (pageCurrent < 1) {
        pageCurrent = 1;
    }

    var ulContain = document.createElement("ul");
    ulContain.className = 'pagination';

    // info text
    var infoText = "Page " + pageCurrent + "/" + pageCount;
    addItemText(ulContain, infoText, "info")

    // first, after
    if (pageCurrent > 1) {
        //addItemPageLink(ulContain, "<<", 1);
        addItemPageLink(ulContain, "<", parameterKey, pageCurrent - 1);
    }

    // 1->[curr]
    if (pageCurrent <= 4) {
        //123[4]
        for (var i = 1; i <= pageCurrent - 1; i++) {
            addItemPageLink(ulContain, i, parameterKey, i);
        }
    }
    else {
        //1...34[5]
        addItemPageLink(ulContain, 1, parameterKey, 1);
        addItemPageLink(ulContain, "...", parameterKey, parseInt(pageCurrent / 2));
        for (var i = pageCurrent - 2; i <= pageCurrent - 1; i++) {
            addItemPageLink(ulContain, i, parameterKey, i);
        }
    }

    //[curr]
    //addItemPageLink(ulContain, pageCurrent, parameterKey, pageCurrent, "active");
    addItemText(ulContain, pageCurrent, "active");

    // [curr]->count
    if (pageCurrent >= pageCount - 4) {
        //[-4]-3-2-1-0
        for (var i = pageCurrent + 1; i <= pageCount; i++) {
            addItemPageLink(ulContain, i, parameterKey, i);
        }
    }
    else {
        //[-6]-5-4-3...0
        for (var i = pageCurrent + 1 ; i <= pageCurrent + 2; i++) {
            addItemPageLink(ulContain, i, parameterKey, i);
        }
        addItemPageLink(ulContain, "...", parameterKey, parseInt((pageCount + pageCurrent) / 2));
        addItemPageLink(ulContain, pageCount, parameterKey, pageCount);
    }

    // first, after
    if (pageCurrent < pageCount) {
        addItemPageLink(ulContain, ">", parameterKey, pageCurrent + 1);
        //addItemPageLink(ulContain, ">>", pageCount);
    }

    //Page 1 of 5
    //addItemPageInput(ulContain, pageCurrent + " of " + pageCount, "", "page-enter");
    //addItemPageTextArea(ulContain, pageCurrent + " of " + pageCount, "", "page-enter");

    return ulContain;
}

function addItemPageLink(parentElement, linkText, parameterKey, parameterPage, className) {
    var linkHref = updateQueryStringParameter(document.URL, parameterKey, parameterPage);
    addItemLink(parentElement, linkText, linkHref, className);
}

function addItemLink(parentElement, linkText, linkHref, className) {
    var tagLink = linkHref;
    var tagText = linkText;

    var liElement = document.createElement("li");
    if (className)
        liElement.className = className;

    var aElement = document.createElement("a");
    aElement.innerHTML = tagText;
    aElement.setAttribute("href", tagLink);
    liElement.appendChild(aElement);

    parentElement.appendChild(liElement);
}

function addItemText(parentElement, innerText, className) {
    var tagText = innerText;

    var liElement = document.createElement("li");
    if (className)
        liElement.className = className;

    var spanElement = document.createElement("span");
    spanElement.innerHTML = tagText;
    liElement.appendChild(spanElement);

    parentElement.appendChild(liElement);
}

//function addItemPageInput(parentElement, linkText, liClassName, inputClassName) {
//    var tagText = linkText;

//    var liElement = document.createElement("li");
//    if (liClassName)
//        liElement.className = liClassName;

//    var inputElement = document.createElement("input");
//    inputElement.type = "text";
//    inputElement.onkeypress = pageInputKeyPress;
//    if (inputClassName)
//        inputElement.className = inputClassName;

//    inputElement.value = tagText;
//    liElement.appendChild(inputElement);

//    parentElement.appendChild(liElement);
//}

//function addItemPageTextArea(parentElement, linkText, liClassName, inputClassName) {
//    var tagText = linkText;

//    var liElement = document.createElement("li");
//    if (liClassName)
//        liElement.className = liClassName;

//    var textElement = document.createElement("textarea");
//    textElement.type = "text";
//    textElement.setAttribute("rows", 1);
//    textElement.setAttribute("cols", 30);
//    textElement.onkeypress = pageInputKeyPress;
//    if (inputClassName)
//        textElement.className = inputClassName;

//    textElement.value = tagText;
//    liElement.appendChild(textElement);

//    parentElement.appendChild(liElement);
//}

//function pageInputKeyPress(evt) {
//    if (evt.keyCode === 13) {
//        var pageIndex = parseInt(this.value);
//        if (pageIndex) {
//            var link = updateQueryStringParameter(document.URL, 'page', pageIndex);
//            location.href = link;
//        }
//    }
//}
